package com.exchange.web.comment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exchange.jwt.JwtUtils;
import com.exchange.utils.ResultUtils;
import com.exchange.utils.ResultVo;
import com.exchange.web.comment.entity.CommentList;
import com.exchange.web.comment.entity.comment;
import com.exchange.web.comment.entity.vo.CommentVO;
import com.exchange.web.comment.entity.vo.CommentsVO;
import com.exchange.web.comment.service.CommentService;
import com.exchange.web.wx_user.entity.WxUser;
import com.exchange.web.wx_user.service.WxUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author G511677
 * @Description TODO
 * @Date 1/6/2024 下午11:30
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private JwtUtils jwtUtils;


    //小程序发布商品评论
    @PostMapping("/add")
    public ResultVo add(@RequestBody comment comment, HttpServletRequest request) {
        comment.setCreateDate(new Date());
        try {
            WxUser wxUser = wxUserService.getById(comment.getUserId());
            if (wxUser == null) {
                return ResultUtils.error("未登录！请登录后操作");
            }
        } catch (Exception e) {
            return ResultUtils.error("未登录！请登录后操作");
        }

//        try {
//            String userId = jwtUtils.jwtDecode(request.getHeader("token")).getClaim("userId").asString();
//            comment.setUserId(Integer.valueOf(userId));
//        } catch (Exception e) {
//            return ResultUtils.error("未登录！请登录后操作");
//        }
//        System.out.println(userId);

        if (commentService.save(comment)) {
            return ResultUtils.success("评论成功");
        } else {
            return ResultUtils.error("评论失败");
        }
    }

    //后台管理获取评论列表
    @PostMapping("/getList")
    public ResultVo getList(CommentList parm) {

        //构造分页对象
        IPage<comment> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        //构造查询条件
        QueryWrapper<comment> query = new QueryWrapper<>();
        query.lambda().orderByDesc(comment::getCreateDate);
        IPage<comment> list = commentService.page(page, query);
        return ResultUtils.success("查询成功", list);
    }

    /**
     * 删除评论
     *
     * @param commentId
     * @return
     */
    @GetMapping("/delete/{commentId}")
    public ResultVo delete(@PathVariable Long commentId) {
        if (commentService.removeById(commentId)) {
            return ResultUtils.success("删除成功");
        } else {
            return ResultUtils.error("删除失败");
        }
    }


    //小程序获取商品下评论列表
    @GetMapping("/getGoodsComment/{goodsId}")
    public ResultVo getGoodsComment(@PathVariable Long goodsId) {
        List<CommentVO> list = commentService.getGoodsComment(goodsId);
        CommentsVO commentsVO = new CommentsVO();
        commentsVO.setCommentCount(list.size());
        commentsVO.setCommentList(list);
        return ResultUtils.success("查询成功", commentsVO);
    }

    /**
     * 后台获取商品评论
     * @param goodsId
     * @param
     * @param
     * @return
     */
    @GetMapping("/getGoodsComment/{goodsId}/{currentPage}/{pageSize}")
    public ResultVo getGoodsComment(@PathVariable Long goodsId,@PathVariable int currentPage,@PathVariable int pageSize) {
        List<CommentVO> list = commentService.getGoodsComment_(goodsId, currentPage, pageSize);
        int total = commentService.getGoodsComment(goodsId).size();
        CommentsVO commentsVO = new CommentsVO();
        commentsVO.setPages((int) Math.ceil((double) total / pageSize));
        commentsVO.setCommentList(list);
        commentsVO.setCommentCount(total);
        commentsVO.setCurrentPage(currentPage);
        return ResultUtils.success("查询成功", commentsVO);
    }


}
